package com.ease.framework {
   import com.ease.util.ServiceApplicabilityUpdateEvent;

   import flash.utils.Dictionary;


/**
 * This registry keeps track of information about a given service being enabled/disabled
 * for a set of objects.
 */
internal class ServiceApplicabilityRegistry
{
   private var _svcInfoByExcludedTarget:Dictionary = new Dictionary( true );

   /**
    * The associated service.
    */
   public var associatedService:Object;

   /**
    * Applies the change requested by the given event on the given target.
    */
   public final function applyChange( target:Object,
                                      ev:ServiceApplicabilityUpdateEvent ):void {
      if (isPermanentlyDisabled( target )) {
         // We can neither enable, nor change to temp-disable; so return.
         return;
      }
      if (ev.type == ServiceApplicabilityUpdateEvent.SERVICE_ENABLE_REQUESTED) {
         delete _svcInfoByExcludedTarget[target];
         return;
      }
      if (ev.type == ServiceApplicabilityUpdateEvent.SERVICE_DISABLE_REQUESTED) {
         var info:ServiceApplicabilityInfo = new ServiceApplicabilityInfo();
         info.isServicePermanentlyDisabled =
               (ev.options & ServiceApplicabilityUpdateEvent.PERMANENTLY_DISABLED) != 0;
         _svcInfoByExcludedTarget[target] = info;
      }
   }

   /**
    * Indicates whether the service is enabled on the given object.
    */
   public final function isServiceEnabledFor( target:Object ):Boolean {
      var info:ServiceApplicabilityInfo = _svcInfoByExcludedTarget[target];
      return info == null;
   }

   /**
    * Indicates whether the service is permanently disabled on the given object.
    */
   public final function isPermanentlyDisabled( target:Object ):Boolean {
      var info:ServiceApplicabilityInfo = _svcInfoByExcludedTarget[target];
      return info != null && info.isServicePermanentlyDisabled;
   }
} // class

}

class ServiceApplicabilityInfo
{
   public static var defaultInstance:ServiceApplicabilityInfo =
         new ServiceApplicabilityInfo();

   public var isServicePermanentlyDisabled:Boolean = false;
} // class